//
//  MCLoginVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/2.
//

import UIKit
import SnapKit
import QMUIKit
import AutoInch

class MCLoginVc: MCBaseVc {

    var _disPlayTitle:UILabel?
    
    var _otherLabel:UILabel?
        
    var _appleBtn:UIButton!
    
    var isLogin:Bool = false
    
    var agerBtn:UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
        
        //图标UI
        loadSubView()
        
        //登录按钮
        LoginButton()
        
        //同意协议
        getProtol()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    func preferredStatusBarStyle() -> UIStatusBarStyle {
        return UIStatusBarStyle.darkContent
    }
    
    //图标UI
    func loadSubView(){
        
        //固定图标
        let imageView = UIImageView.init()
        imageView.image = UIImage.init(named: "iconlogin_icon_black_image")
        view.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.centerX.equalTo(view.center.x)
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(80.auto())
//            make.width.equalTo(100)
//            make.height.equalTo(100)
        }
            
        //固定文字Master显示
        let displayTitle = UIImageView.init()
        displayTitle.image = UIImage(named: "login_master_icon_image")
        displayTitle.contentMode = .scaleAspectFit
        view.addSubview(displayTitle)
        displayTitle.snp.makeConstraints { make in
            make.centerX.equalTo(view.center.x)
            make.top.equalTo(imageView.snp.bottom).offset(5)
//            make.height.equalTo(60)
//            make.width.equalTo(110)
        }
        
        
        //固定文字中文显示
        let displayCnTitle = UILabel.init()
        displayCnTitle.text = "运动健身与健康"
        displayCnTitle.textColor = UIColor.qmui_color(withHexString: "#FFFFFF")?.withAlphaComponent(0.8)
        displayCnTitle.font = UIFont(name: "PingFangTC-Regular", size: 14)
        view.addSubview(displayCnTitle)
        displayCnTitle.snp.makeConstraints { make in
            make.centerX.equalTo(view.center.x)
            make.top.equalTo(displayTitle.snp.bottom).offset(8)
            make.height.equalTo(20)
        }
        
        _disPlayTitle = displayCnTitle
    }
        
    
    //登录按钮（微信和apple登录）
    func LoginButton() {
        
        ///微信登录
        let weChatBtn = UIButton(type: .custom)
        weChatBtn.setTitle("微信登录", for: .normal)
        weChatBtn.titleLabel?.font = UIFont(name: "PingFangTC-Semibold", size: 15)
        weChatBtn.setTitleColor(UIColor.qmui_color(withHexString: "#10151C"), for: .normal)
        weChatBtn.setImage(UIImage(named: "wechatlogin_wechat_black_image"), for: .normal)
        weChatBtn.backgroundColor = UIColor.qmui_color(withHexString: "#35DE94")
        weChatBtn.layer.cornerRadius = 25
        weChatBtn.addTarget(self, action: #selector(wechateLogin), for: .touchUpInside)
        weChatBtn.isHidden =  !LQShareSDK.isWechatInstall()
      
        view.addSubview(weChatBtn)
        weChatBtn.snp.makeConstraints { make in
            make.top.equalTo(_disPlayTitle!.snp.bottom).offset(80.auto())
            make.centerX.equalTo(view.center.x)
            make.width.equalTo(300.auto())
            make.height.equalTo(50)
        }
        
        
        //apple登录
        let appleBtn = UIButton(type: .custom)
        appleBtn.setTitle("通过Apple登录", for: .normal)
        appleBtn.backgroundColor = UIColor.qmui_color(withHexString: "#10151C")
        appleBtn.layer.borderWidth = 1.0
        appleBtn.setTitleColor(UIColor.qmui_color(withHexString: "#35DE94"), for: .normal)
        appleBtn.titleLabel?.font = UIFont(name: "PingFangTC-Semibold", size: 15)
        appleBtn.layer.borderColor = UIColor.qmui_color(withHexString: "#35DE94")?.cgColor
        appleBtn.setImage(UIImage(named: "login_apple_green_image"), for: .normal)
        appleBtn.layer.cornerRadius = 22.5
        
        view.addSubview(appleBtn)
        appleBtn.snp.makeConstraints { make in
            make.top.equalTo(weChatBtn.snp.bottom).offset(25)
            make.centerX.equalTo(view.center.x)
            make.width.equalTo(300.auto())
            make.height.equalTo(45)
        }
        appleBtn.addTarget(self, action: #selector(appleLogin), for: .touchUpInside)
        _appleBtn = appleBtn
        
        //其他方式登录
        let otherLoginLabel = UILabel.init()
        otherLoginLabel.text = "其他方式登录"
        otherLoginLabel.font = UIFont(name: "PingFangTC-Regular", size: 12)
        otherLoginLabel.textColor = UIColor.white
        otherLoginLabel.isUserInteractionEnabled = true
        view.addSubview(otherLoginLabel)
        otherLoginLabel.snp.makeConstraints { make in
            make.top.equalTo(appleBtn.snp.bottom).offset(50)
            make.centerX.equalTo(view.center.x)
            make.height.equalTo(14)
            
        }
        _otherLabel = otherLoginLabel
        
        let otherTap = UITapGestureRecognizer.init(target: self, action: #selector(otherLoginAction))
        otherLoginLabel.addGestureRecognizer(otherTap)
        
        let enterImageView = UIImageView.init()
        enterImageView.image = UIImage(named: "login_enter_image")
        view.addSubview(enterImageView)
        enterImageView.snp.makeConstraints { make in
            make.left.equalTo(otherLoginLabel.snp.right).offset(3)
            make.centerY.equalTo(otherLoginLabel.snp.centerY)
            make.height.equalTo(10)
            make.width.equalTo(5.3)
        }
        
    }
    
    @objc func wechateLogin() -> Void {
        
        if isLogin == false {
            self.view.makeToast("请同意协议",position: .center)
            return
        }
        
        LQShareSDK.login(.wechat) { uid, name, url, sex in
            
            
            self.WXLogin(code: name)
            
            
        } failed: { error in
            self.view.makeToast("微信登录失败",position: .center)
        }


        
    }
    
    //微信登录
    func WXLogin(code:String?) -> Void {
        NetWorkRequest(MCLoginApi.wechatApi(code: code)) { ResponseModel in
            
            let dataMode = MCData.init(JSONString: ResponseModel.dataString)
            

            if dataMode?.tokenValue?.isEmpty == true {
               
                let mobileVc = MCLoginMobileVc.init()
                mobileVc.binding = true
                mobileVc.datModel = dataMode
                self.navigationController?.pushViewController(mobileVc, animated: true)
                
            }else{
                UserDefaults.standard.set(dataMode?.tokenValue ?? "", forKey: "LoginToken")
                UserDefaults.standard.set(dataMode?.tokenName, forKey: "LoginName")
                let appelete:AppDelegate = UIApplication.shared.delegate as! AppDelegate
                 
                appelete.tabbarInfo()
                
            }
            
        } failureCallback: { ResponseModel in
            self.view.makeToast("登录失败",position: .center)
        }
    }
    
        
    //苹果登录
    @objc func appleLogin() {
        
        if isLogin == false {
            self.view.makeToast("请同意协议",position: .center)
            return
        }
        
        
        LQAppleLogin().loginWithCompleteHandler { successed, user, familyName, givenName, email, password, identityToken, authorizationCode, error, msg in
           
            self.appleApiData(nickName: user, identifierToken: String(data: identityToken ?? Data(), encoding: .utf8) ?? "")
            
            print(msg)
        }
    }
    
    
    func appleApiData(nickName:String?,identifierToken:String?) -> Void {
        NetWorkRequest(MCLoginApi.appleApi(nickName:nickName, identiftyToken: identifierToken)) { ResponseModel in
         
            
            let dataMode = MCData.init(JSONString: ResponseModel.dataString)
            

            if dataMode?.tokenValue?.isEmpty == true {
               
                let mobileVc = MCLoginMobileVc.init()
                mobileVc.binding = true
                mobileVc.datModel = dataMode
                self.navigationController?.pushViewController(mobileVc, animated: true)
                
            }else{
                UserDefaults.standard.set(dataMode?.tokenValue ?? "", forKey: "LoginToken")
                UserDefaults.standard.set(dataMode?.tokenName, forKey: "LoginName")
                let appelete:AppDelegate = UIApplication.shared.delegate as! AppDelegate
                 
                appelete.tabbarInfo()
                
            }
            
        } failureCallback: { ResponseModel in
            self.view.makeToast("网络失败",position: .center)
        }
    }
    
    
    //其他登录方式
    @objc func otherLoginAction(){
        let mobileVc = MCLoginMobileVc.init()
        self.navigationController?.pushViewController(mobileVc, animated: true)
    }
    
    
    
    //协议功能
    func getProtol() {
        //用户协议文字
        let protolLabel = UILabel.init()
        protolLabel.text = "《用户协议》"
        protolLabel.font = UIFont(name:"PingFangSC-Regular", size: 12)
        protolLabel.textColor = UIColor.qmui_color(withHexString:"#FFFFFF")
        view.addSubview(protolLabel)
        protolLabel.snp.makeConstraints { make in
//            make.left.equalTo(agreeLabel.snp.right).offset(0)
//            make.centerY.equalTo(agreeLabel.snp.centerY).offset(0)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-30)
            make.height.equalTo(14)
            make.right.equalTo(view.snp.centerX).offset(20)
        }
        
        
        //和文字
        let andLabel = UILabel.init()
        andLabel.text = "和"
        andLabel.font = UIFont(name:"PingFangSC-Regular", size: 12)
        andLabel.textColor = UIColor.qmui_color(withHexString:"#FFFFFF")?.withAlphaComponent(0.5)
        view.addSubview(andLabel)
        andLabel.snp.makeConstraints { make in
            make.left.equalTo(protolLabel.snp.right).offset(0)
            make.centerY.equalTo(protolLabel.snp.centerY).offset(0)
            make.height.equalTo(14)
            
        }
        
        
        //隐私政策文字
        let privacyLabel = UILabel.init()
        privacyLabel.text = "《隐私政策》"
        privacyLabel.font = UIFont(name:"PingFangSC-Regular", size: 12)
        privacyLabel.textColor = UIColor.qmui_color(withHexString:"#FFFFFF")
        view.addSubview(privacyLabel)
        privacyLabel.snp.makeConstraints { make in
            make.left.equalTo(andLabel.snp.right).offset(0)
            make.centerY.equalTo(andLabel.snp.centerY).offset(0)
            make.height.equalTo(14)
            
        }
        
        
        //同意文字
        let agreeLabel = UILabel.init()
        agreeLabel.text = "同意"
        agreeLabel.font = UIFont(name:"PingFangSC-Regular", size: 12)
        agreeLabel.textColor = UIColor.qmui_color(withHexString:"#FFFFFF")?.withAlphaComponent(0.5)
        view.addSubview(agreeLabel)
        agreeLabel.snp.makeConstraints { make in
            make.right.equalTo(protolLabel.snp.left)
            make.centerY.equalTo(protolLabel.snp.centerY)
            make.height.equalTo(14)
        }
        
        
        //选择同意的按钮
        let selectAgreeBtn = UIButton.init()
        selectAgreeBtn.setImage(UIImage(named:"login_unselect_white_image"), for: .normal)
        selectAgreeBtn.addTarget(self, action: #selector(agereeBtn(agerSender:)), for: .touchUpInside)
        view.addSubview(selectAgreeBtn)
        agerBtn = selectAgreeBtn
        selectAgreeBtn.snp.makeConstraints { make in
            make.right.equalTo(agreeLabel.snp.left).offset(-2)
//            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-30)
            make.height.width.equalTo(40)
            make.centerY.equalTo(agreeLabel)
        }
        
        
    }
    
    
    @objc func agereeBtn(agerSender:UIButton) -> Void {
        agerSender.isSelected = !agerSender.isSelected
        
        isLogin = agerSender.isSelected
        
        if agerSender.isSelected == true {
            agerSender.setImage(UIImage(named: "login_agree_white_select_image"), for: .selected)
        }else{
            agerSender.setImage(UIImage(named: "login_unselect_white_image"), for: .normal)
        }
    
    }
   
    
}
